<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2023 yoncron All rights reserved.
// +----------------------------------------------------------------------
// | Author: yoncron <yoncron@163.com>
// +----------------------------------------------------------------------
namespace app\controller\app\im;

use app\controller\app\AppBase;
use app\model\FriendsModel;
use app\model\UserModel;
use app\service\Route;
use support\Response;

class Friends extends AppBase
{
    public function beforeAction(){
        $rtn = parent::beforeAction();
        if($rtn instanceof Response){
            return $rtn;
        }
    }
    /**
     * description:联系人列表
     * @Route(reqUrl="/im.friends.lst")
     * author:yoncron<yoncron@163.com>
     * data:2023/10/31
     */
    public function lst(){
        $where=[];
        $where[]=['mine_user_id','=',$this->user_id];
        $friendsObj = new FriendsModel();
        $res = $friendsObj->lst($where);
        return json($res);
    }

    /**
     * description:查找用户
     * @Route(reqUrl="/im.contact.search")
     * author:yoncron<yoncron@163.com>
     * data:2023/11/04
     */
    public function search(){
        if(input('keyword',null)===null){
            return error(1,'请输入账号或手机号');
        }
        $where=[];
        $where[] = ['mobile|usercode','=',input('keyword')];
        $where[]=['user_id','<>',$this->user_id];
        $where[]=['user_id','<>',10000];
        $where[]=['useable','=',1];
        $where[]=['deleted','=',0];
        $userObj = new UserModel();
        $user = $userObj->field('user_id,usercode,mobile,username,avatar,sex,online_status')->where($where)->find();
        if($user){
            $friendsObj = new FriendsModel();
            $is_friend = $friendsObj->where([['mine_user_id','=',$this->user_id],['friend_user_id','=',$user['user_id']]])->find();
            if($is_friend){
                $user['is_friend']=1;
            }
            else{
                $user['is_friend']=0;
            }
            $user['keyword'] = input('keyword');
        }
        $rtn['info']=$user;
        return success($rtn);
    }
}